package com.my.controller;

import com.my.mapper.RightMapper;
import com.my.pojo.Right;
import com.my.pojo.RightColumn;
import com.my.pojo.RightColumnMapper;
import com.my.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.serial.SerialException;
import java.io.IOException;
import java.util.List;

@WebServlet("/right")
public class RightController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
      req.setCharacterEncoding("ut-f");
      HttpSession session = req.getSession();
      resp.setContentType("text/html;charset=utf-8");
      String action = req.getParameter("action");
      String result ="";
      SqlSession sqlSession = MyBatisUtil.openSession();
      if(action.equals("preAdd")){
          RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
          List<RightColumn> rightColumnList = rightColumnMapper.searcherAllEightColumn();
          if(rightColumnList == null){
              result ="right/addRightCol";
          }else{
              session.setAttribute("allRightColumn",rightColumnList);
              result = "right/addRightColumn";
          }
      }
      if(action.equals("add")){
          String rightColumnIdStr = req.getParameter("sightColumn");
          int rightColumnId = Integer.parseInt(rightColumnIdStr);
          String rightName =req.getParameter("rightName");
          String rightDesc =req.getParameter("rightDesc");
          String url = req.getParameter("url");
          Right right = Right.builder().rightColumnId(rightColumnId).rightName(rightName).rightDesc(rightDesc).url(url).build();
          RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
          Boolean existRight;
      }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
